<?php
	class Thanhtoan extends CI_Controller
	{
		function paypal()
		{
			
			$ngaybatdau = $this->input->post('ngaybatdau');
			$thangbatdau = $this->input->post('thangbatdau');
			$nambatdau = $this->input->post('nambatdau');
	
			$ngayketthuc = $this->input->post('ngayketthuc');
			$thangketthuc = $this->input->post('thangketthuc');
			$namketthuc = $this->input->post('namketthuc');
	
			$sotienthemnguoihienhanh = $this->input->post('sotienthemnguoi');
			$songuoilonmacdinh = $this->input->post('songuoilonmacdinh');
			$sophong = $this->input->post('numberRooms');
	
			$khachsan = $this->input->post('makhachsan');
			$loaiphong = $this->input->post('loaiphong');
	
			$phantramgiam = 0;//$this->input->post('phantramgiam');
			$diem = $this->input->post('diem');
			$sothe = $this->input->post('sothe');
	
			$gionhanphong = $this->input->post('eta');
			$email = $this->input->post('emailAddress');
			$email = str_replace("@","..",$email);
			$dienthoai = $this->input->post('phoneNumber');
	
			$ghichu = $this->input->post('comments');
			
			$ghichu = trim($ghichu);
			if($ghichu == "") $ghichu = "NULL";
			
			$tongtien = $this->input->post('totalconlai');
			$tinhtrangthanhtoan = 1;//$this->input->post('tinhtrangthanhtoan');
			$data = "";
			for ($i = 1; $i <= $sophong; $i++) {
				$songuoilon = $this->input->post('room' . $i . 'NumberAdults');
				$sotreem = $this->input->post('room' . $i . 'NumberChildren');
				$ho = $this->input->post('room' . $i . 'GuestLastName');
				$ten = $this->input->post('room' . $i . 'GuestFirstName');
            	$loaigiuong = $this->input->post('room' . $i . 'Option');
	
				$data .= "/$songuoilon/$sotreem/$ho/$ten/$loaigiuong";
			}
			
			$this->load->library('paypal_class');
			$this->paypal_class->paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';   // testing paypal url
			//$this->paypal_class->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';     // paypal url
			$this->paypal_class->add_field('currency_code', 'USD');
			$this->paypal_class->add_field('business', 'seller_1357435594_biz@gmail.com');
			//$this->paypal_class->add_field('business', $this->config->item('bussinessPayPalAccount'));
			$this->paypal_class->add_field('return', site_url('home')); // return url
			$this->paypal_class->add_field('cancel_return', site_url('home')); // cancel url
			$this->paypal_class->add_field('notify_url', site_url("thanhtoan/validatepaypal/$ngaybatdau/$thangbatdau/$nambatdau/$ngayketthuc/$thangketthuc/$namketthuc/$sotienthemnguoihienhanh/$songuoilonmacdinh/$sophong/$khachsan/$loaiphong/$phantramgiam/$diem/$sothe/$gionhanphong/$email/$dienthoai/$ghichu/$tongtien/$tinhtrangthanhtoan".$data)); // notify url			
			
			$totalPrice = $this->input->post('totalPrice');
			$this->paypal_class->add_field('item_name', 'Room booking');
			$this->paypal_class->add_field('amount', $totalPrice);
			$this->paypal_class->add_field('custom', 10);
			$this->paypal_class->submit_paypal_post(); // submit the fields to paypal
			//$this->paypal_class->dump_fields();      // for debugging, output a table of all the fields
			exit;	
			
		}
		
		function validatepaypal()
		{
				$this->load->model('phieudatphong_model');
				$this->load->model('chitietphieudat_model');
				$this->load->model('giathuetheongay_model');
				$this->load->model('phongtrongtheongay_model');
				$this->load->model('thekhuyenmai_model');
				//$ts = mktime(0, 0, 0, 12, 31, 2012);
		//        $date = date("Y-m-d", $ts);
		//        $date = date_create($date);
		//        date_add($date, date_interval_create_from_date_string('1 days'));
		//        echo $date;
				$ngaybatdau = $this->uri->segment(3);
				$thangbatdau = $this->uri->segment(4);
				$nambatdau = $this->uri->segment(5);
		
        $ngayketthuc = $this->uri->segment(6);
        $thangketthuc = $this->uri->segment(7);
        $namketthuc = $this->uri->segment(8);

        $ts = mktime(0, 0, 0, $thangbatdau, $ngaybatdau, $nambatdau);
        $startdate = date("Y-m-d", $ts);
        $startdate = date_create($startdate);

        $ts = mktime(0, 0, 0, $thangketthuc, $ngayketthuc, $namketthuc);
        $enddate = date("Y-m-d", $ts);
        $enddate = date_create($enddate);

        $sotienthemnguoihienhanh = $this->uri->segment(9);
        $songuoilonmacdinh = $this->uri->segment(10);
        $sophong = $this->uri->segment(11);

        $khachsan = $this->uri->segment(12);
        $loaiphong = $this->uri->segment(13);

        $phantramgiam = $this->uri->segment(14);
        $diem = $this->uri->segment(15);
        $sothe = $this->uri->segment(16);

        $gionhanphong = $this->uri->segment(17);
        $email = $this->uri->segment(18);
        $dienthoai = $this->uri->segment(19);

        $ghichu = $this->uri->segment(20);
        $tongtien = $this->uri->segment(21);
        $tinhtrangthanhtoan = $this->uri->segment(22);

        $ngaydatphong = date("Y-m-d H:i:s");
		$email = str_replace("..","@",$email);
        $data = array(
            'Email' => $email,
            'DienThoai' => $dienthoai,
            'NgayDatPhong' => $ngaydatphong,
            'GioNhanPhong' => $gionhanphong,
            'PhanTramGiam' => $phantramgiam,
            'TongTien' => floor($tongtien/1000),
            'TinhTrangThanhToan' => $tinhtrangthanhtoan,
            'LoaiPhong' => $loaiphong);
        if ($sothe != 0)
        {
			$data['SoThe'] = $sothe;
			$this->thekhuyenmai_model->update_records_the_i12($tongtien);
		}
        if ($ghichu != '')
            $data['GhiChu'] = $ghichu;

        $id = $this->phieudatphong_model->add_records_phieudatphong($data);
		$temp = 23;
        for ($i = 1; $i <= $sophong; $i++) {
            $songuoilon = $this->uri->segment($temp++);
            $sotreem = $this->uri->segment($temp++);
            $ho =  $this->uri->segment($temp++);
            $ten =  $this->uri->segment($temp++);
            $loaigiuong =  $this->uri->segment($temp++);

            if ($songuoilon > $songuoilonmacdinh)
                $sotienthemnguoi = $sotienthemnguoihienhanh * ($songuoilon - $songuoilonmacdinh);
            else
                $sotienthemnguoi = 0;
				
			
           $datact = array(
                'HoTenNguoiDaiDien' => $ho . ' ' . $ten,
                'SoNguoiLon' => $songuoilon,
                'SoTreEm' => $sotreem,
                'SoTienThemNguoi' => $sotienthemnguoi / 1000,
                'LoaiGiuong' => $loaigiuong,
                'PhieuDatPhong' => $id);
            $this->chitietphieudat_model->add_records_chitietdatphong($datact);
            $this->phongtrongtheongay_model->update_records_phongtrongtheongay($startdate, $enddate, $loaiphong, $loaigiuong);
        }
		
		$ts = mktime(0, 0, 0, $thangbatdau, $ngaybatdau, $nambatdau);
        $startdate = date("Y-m-d", $ts);
        $startdate = date_create($startdate);

        $ts = mktime(0, 0, 0, $thangketthuc, $ngayketthuc, $namketthuc);
        $enddate = date("Y-m-d", $ts);
        $enddate = date_create($enddate);
        $this->giathuetheongay_model->add_records_giathuetheongay($startdate, $enddate, $id, $loaiphong);
		if($tinhtrangthanhtoan == 1)
		{
		// Gửi email thanh toán thành công
					$emailfrom = 'i12.onlinebooking@gmail.com';
					$emailpass = 'onlinebooking';
					$sender = 'i12 Online Booking';
				
					$emailto = $this->phieudatphong_model->LayEmail($id);
					$subject = "Đặt phòng thành công";
					$message = "Chúc mừng bạn, bạn đã đăng ký và thanh toán thành công. \r\n Công ty xin chân thành cảm ơn bạn đã đặt niềm tin nơi chúng tôi. \r\n Mã số đặt phòng của bạn là ".$id.". Vui lòng ghi nhớ mã đặt phòng này!!!";
				
					$config = array(
									'protocol' => 'smtp',
									'smtp_host' => 'ssl://smtp.googlemail.com',
									'smtp_port' => 465,
									'smtp_user' => $emailfrom,
									'smtp_pass' => $emailpass,
									'wordwrap' => FALSE
									);
								
					$this->load->library('email', $config);
					$this->email->set_newline("\r\n");
				
					$this->email->from($emailfrom, $sender);
					$this->email->to($emailto);
					$this->email->subject($subject);
					$this->email->message($message);
				
					if($this->email->send())
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
						<script>
							alert("Thanh toán thành công. Thư xác nhân đặt phòng đã được gửi đi.");
							window.location = "<?php echo base_url(); ?>index.php/datphong/chitiet/<?php echo $id; ?>";
						</script>
						<?php					
					}
					else
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
						<script>
							alert("Gửi thư xác nhận đặt phòng thất bại!");
							window.location = "<?php echo base_url(); ?>index.php/datphong/chitiet/<?php echo $id; ?>";
						</script>
						<?php					
					}
				}
				else
				{
					redirect('datphong/chitiet/'.$id);
				}
			}
		}
	
?>